<?php
//作者千柒qq743395324
namespace Admin\Controller;

use Think\Controller;

class WebDataController extends Controller
{
	public function _initialize(){
            //判断用户是否已经登录
        if (!isset($_SESSION['username'])) {
         redirect('Admin/Login');
         exit;
        }
    }
    public function index()
    {
        $sql = 
        cookie('url',$_SERVER['REQUEST_URI']);
        $data = M('classify')->order('id desc')->where(array('class_sid'=>0))->select();
        $tot=M('webdata')->where()->count();//where(array('type'=>0))->
        //实例化分页类
        $page=new \Think\Page($tot,10);//分页设置

        $web = M('webdata')->limit($page->firstRow.','.$page->listRows)->order('id desc')->where(array('uid'=>0))->select();
        $this->assign('data',$data);
        $this->assign('web',$web);
        $this->show=$page->show();

        $this->display();
    }
    //添加
    public function web_post()
    {
        $id = I('post.id','','htmlspecialchars');
        $title = I('post.title','','htmlspecialchars');
        $keywords = I('post.keywords','','htmlspecialchars');
        $description = I('post.description','','htmlspecialchars');
        $sid = I('post.sid','','htmlspecialchars');
        $url = I('post.url','','htmlspecialchars');
        if(!$title){
            exit('请输入网站标题');
        }
        if(!$sid){
            exit('请选择分类');
        }
        $data = array(
            'title'=>$title,
            'keywords'=>$keywords,
            'description'=>$description,
            'url'=>$url,
            'sid'=>$sid,
            'uptime'=>strtotime(date("Y/m/d H:i:s")),
            'type'=>1,
            'uid'=>0,
            'click'=>0,
        );
        $sql = M('webdata');
        if($id){
            if($sql->where('id='.$id)->save($data)){
                    echo '操作成功';
                }else{
                    echo '操作失败';
                }
        }else{
            if($sql->add($data)){
                echo '操作成功';
            }else{
                echo '操作失败';
            }
        }

    }
    //删除
     public function web_del()
    {
         $id = I('post.id','','htmlspecialchars');
        if(!$id){
            exit('非法操作');
        }
        if(M('webdata')->where('id='.$id)->delete()){
        echo '操作成功';
        }else{
            echo '操作失败';
        }
        
    }
    //改变状态
     public function web_type()
    {
         $id = I('post.id','','htmlspecialchars');
         if($id){
            $web = M('webdata')->where('id='.$id)->find();
            if($web['type']==1){
                if(M('webdata')->where('id='.$id)->save(array('type'=>0))){
                    echo '操作成功';
                }else{
                    echo '操作失败1';
                }
            }else{
                if(M('webdata')->where('id='.$id)->save(array('type'=>1))){
                    echo '操作成功';
                }else{
                    echo '操作失败2'.$web['type'];
                }
            }
         }
        
    }
    //获取网站信息
     public function web_info()
    {
        $url = I('post.url','','htmlspecialchars');
        $code = I('post.code','','htmlspecialchars');
        if(!$url){
            exit('请输入网址');
        }
        $html = file_get_contents($url);
        if($code==1){
            $html = iconv("GBK", "utf-8",$html);//防乱码
        }
        preg_match("/<title>(.*)<\/title>/i",$html, $title);
        preg_match('/<meta name="keywords" content="(.*)">/i', $html, $keywords);
        preg_match('/<meta name="description" content="(.*)">/i', $html, $description);   
        $data = array(
            'titles'=>$title[1],
            'keywords'=>$keywords[1],
            'description'=>$description[1],
        );
        echo json_encode($data);
        
    }
    public function web_so()
    {
        $word = I('post.word','','htmlspecialchars');

        $where['title'] = array('like','%'.$word.'%');
        $web = M('webdata')->where($where)->select();
        $data = M('classify')->order('id desc')->where('class_uid=0')->select();
        $this->assign('web',$web);
        $this->assign('data',$data);
        $this->display();
    }

}
